package com.qf.dao.daoImpl;

import com.qf.dao.loginDao;
import com.qf.pojo.Profile;
import com.qf.pojo.User;
import com.qf.utils.DButil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class loginDaoImpl implements loginDao {
    private QueryRunner queryRunner=new QueryRunner(DButil.getDateSource());
    @Override
    public User login(String username,String password) {
        String sql="select * from user where username =? and password=?";
        try {
            User login = queryRunner.query(sql, new BeanHandler<User>(User.class),username,password);
            return login;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public int add(String username,String password) {
        String sql="insert into user(username,password) values(?,?)";
        try {
            int update = queryRunner.update(sql, new BeanHandler<User>(User.class), username, password);
            return update;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Profile> AllDao() {
        String sql="selet * from profile";
        try {
            List<Profile> list = queryRunner.query(sql, new BeanListHandler<Profile>(Profile.class));
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public boolean delectDao(String id) {
        String sql="delect from profile where id=?";
        try {
            queryRunner.update(sql,id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

}
